package com.ccp.dev.workflow.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.flowable.ui.common.service.exception.InternalServerErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassDescribe: json模板文件解析类
 * @author :wangcheng
 * Date: 2019-05-06
 */
@RestController
@RequestMapping("/setting")
public class FlowStencilSetResource {
    private static final Logger LOGGER = LoggerFactory.getLogger(FlowStencilSetResource.class);

    @Resource
    protected ObjectMapper objectMapper;

    /**
     * 加载流程设计器文件
     * @param response http响应
     */
    @RequestMapping(value = "/rest/stencilSets/editor", method = RequestMethod.GET, produces = "application/json")
    public void getStencilSetForEditor(HttpServletResponse response) {
        try {
            JsonNode jsonNode= objectMapper.readTree(this.getClass().getClassLoader().getResourceAsStream("stencilset/stencilset_bpmn.json"));
            response.setCharacterEncoding("UTF-8");
            response.getWriter().print(jsonNode);
        } catch (Exception e) {
            LOGGER.error("Error reading bpmn stencil set json", e);
            throw new InternalServerErrorException("Error reading bpmn stencil set json");
        }
    }
}
